$(function () {
    //初始化加载数据
    $("#jqGrid").jqGrid({
        url: baseURL + 'post/list',
        datatype: "json",
        //数据字段一一绑定 <> 根据name
        colModel: [
            {label: 'Id', name: 'id', index: "id", width: 45, key: true},
            {label: '岗位编码', name: 'code', index: "code", width: 75},
            {label: '岗位名称', name: 'name', index: "name", width: 75},
            {label: '描述', name: 'remark',index:"description", width: 100},
            {label: '排序编号', name: 'orderNumber', index: "order_number", width: 75},
            {
                label: '状态', name: 'enable', width: 60, formatter: function (value, options, row) {
                    return value === false ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }
            },
            {label: '创建人', name: 'createname', index: "createname", width: 45},
            {label: '创建时间', name: 'createTime', index: "create_time", width: 85},
            {label: '修改人', name: 'updatename', index: "updatename", width: 45},
            {label: '修改时间', name: 'updateTime', index: "update_time", width: 85},
        ],
        viewrecords: true,
        height: 425,
        rowNum: 10,     //页面显示的数量
        rowList: [10, 20, 40, 60, 80, 100, 120, 200],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,  //是否可多选
        pager: "#jqGridPager",
        //读取服务器返回的json数据并解析
        jsonReader: {
            root: "data.page.list",
            page: "data.page.currPage",     //初始页
            total: "data.page.totalPage",
            records: "data.page.totalCount"
        },
        //设置jqGrid将要向服务端传递的参数名称
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});
//vue对象
var vm = new Vue({
    el: '#pmpapp',
    data: {
        q: {
            name: null
        },
        showList: true,
        title: null,
        postList: {},
        post: {
            id: null,
            code: null,
            name: null,
            orderNumber: 0,
            enable: 1,
            remark: null
        }
    },
    //请求方法
    methods: {
        //查询
        query: function () {
            vm.reload();
        },

        //重新load数据时的请求方法
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'search': vm.q.name},    //获取输入框的内容，将值赋给search
                page: page
            }).trigger("reloadGrid");
        },

        //重置
        reset: function () {
            window.location.href = baseURL + "modules/sys/post";
        },

        //进入新增
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.post = {code: null, name: null, enable: true, remark: null};
        },

        //进入修改0
        update: function () {
            //获取当前修改的id
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },

        //获取详情
        getInfo: function (id) {
            $.get(baseURL + "post/info?id=" + id, function (r) {
                vm.post = r.data.post;
            });
        },

        //删除
        del: function () {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var password =   prompt("删除前请确定输入密码：");
            $.ajax({
                async:false,
                type: "POST",
                url: baseURL + "sysManagement/password",
                contentType: "application/json",
                data: password,
                success: function (r) {
                    if (r.code == 0) {
                        confirm('确定要删除选中的记录？', function () {
                            var data = JSON.stringify(ids);
                            $.ajax({
                                async:false,
                                type: "POST",
                                url: baseURL + "post/deletePost",
                                contentType: "application/json",
                                data: data,
                                success: function (r) {
                                    if (r.code == 0) {
                                        alert('操作成功', function () {
                                            vm.reload();
                                        });
                                    } else {
                                        alert(r.msg);
                                    }
                                }
                            });
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },

        //保存-更新数据
        saveOrUpdate: function () {
            //如果id为空则为添加，否则为修改
            var url = vm.post.id == null ? "post/save" : "post/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.post),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        }

    }
});